
#include <stdio.h>      // 标准输入输出
#include <unistd.h>     // POSIX标准接口

#include "ohos_init.h"
#include "cmsis_os2.h"

#include "iot_gpio.h"
#include "hi_io.h"
#include "hi_adc.h"

#define OXYGEN_SENSOR_CHAN_NAME HI_ADC_CHANNEL_2    //gpio05

static float ConvertToVoltage(unsigned short data)
{
    return (float)data * 1.8 * 4 / 4096;
}

float GetOxygenLevel ()
{
    unsigned short data = 0;//获取数据

    if (hi_adc_read(OXYGEN_SENSOR_CHAN_NAME,&data,HI_ADC_EQU_MODEL_4,
                    HI_ADC_CUR_BAIS_DEFAULT,0)==HI_ERR_SUCCESS)
    {
        float VX = ConvertToVoltage(data);//电压


        float oxygen_sensor=VX*53.59-3.2129;
        printf("ADC_VALUE=%d VX=%f oxygen_sensor=%f\n",data,VX,oxygen_sensor);
        return oxygen_sensor;
    }

    return 0;//读取失败
}